/**

   net_proto.h
   -----------

   Network protocol

**/


#ifndef NET_PROTO_H
#define NET_PROTO_H


/**

   Includes
   --------

   - stdint.h	: uintX_t

**/


#include <stdint.h>



/**

   Constants: Action IDs
   ---------------------

**/


#define ID_OPEN                         1
#define ID_OPEN_RESPONSE                2
#define ID_CHUNK_LOCATION               3
#define ID_CHUNK_LOCATION_RESPONSE      4
#define ID_CHUNK_READ                   5
#define ID_CHUNK_READ_RESPONSE          6
#define ID_CHUNK_WRITE                  7
#define ID_CHUNK_WRITE_RESPONSE         8
#define ID_CLOSE                        9
#define ID_CLOSE_RESPONSE               10
#define ID_HEARTBEAT                    11
#define ID_HEARTBEAT_RESPONSE           12
#define ID_REGISTER                     13
#define ID_REGISTER_RESPONSE            14
#define ID_CHUNK_QUERY                  15
#define ID_CHUNK_QUERY_RESPONSE         16



/**

   Structure: struct net_proto_header
   ----------------------------------

   Common Header. Members are:
   - id  : Action ID

**/

struct net_proto_header
{
  uint8_t id;
};



/**

   Structure: struct net_proto_open
   --------------------------------

   Members are:
   - namelen  : file name len
   - filename : filename
   - mode     : opening mode (r,rw,a,...)

**/

struct net_proto_open
{
  uint16_t namelen;
  char* filename;
  uint8_t mode;
};


/**

   Structure: struct net_proto_open_r
   ----------------------------------

   Members are:
   - filesize : size in bytes
   - handle   : handle to the requested file

**/

struct net_proto_open_r
{
  uint64_t filesize;
  uint32_t handle;
};


/**

   Structure: struct net_proto_chunk_location
   ------------------------------------------

   Members are :
   - handle  : file handle returned by open
   - nchunks : number of chunks
   - chunks  : list of chunk numbers.

**/

struct net_proto_chunk_location
{
  uint32_t handle;
  uint32_t nchunks;
  uint32_t* chunks;
};


/**

   Structure: struct net_proto_chunk_location_r
   --------------------------------------------

   Members are :
   - nchunks : number of chunks
   - chunks  : list of chunk numbers.
   - uuids   : list of chunk uuids.
   - ipaddrs : list of chunk servers' ip address'
   - ports   : list of chunk servers' ports

**/

struct net_proto_chunk_location_r
{
  uint32_t nchunks;
  uint32_t* chunks;
  uint64_t* uuids;
  uint32_t* ipaddrs;
  uint16_t* ports;
};


/**

   Structure: struct net_proto_read
   --------------------------------

   Members are :
   - uuid  : Chunk uuid to read
   - start : first byte to read
   - end   : last byte to read

**/


struct net_proto_read
{
  uint64_t uuid;
  uint32_t start;
  uint32_t end;
};


/**

   Structure: struct net_proto_read_r
   ----------------------------------

   Members are :
   - uuid     : chunk uuid
   - checksum : data read checksum
   - size     : data size
   - data     : data read

**/


struct net_proto_read_r
{
  uint64_t uuid;
  uint32_t checksum;
  uint32_t size;
  uint8_t* data;
};


/**

   Structure: struct net_proto_write
   ---------------------------------

   Members are :
   - uuid     : Chunk uuid to read
   - start    : first byte to read
   - end      : last byte to read
   - checksum : data to write checksum
   - data     : data to write

**/


struct net_proto_write
{
  uint64_t uuid;
  uint32_t start;
  uint32_t end;
  uint32_t checksum;
  uint8_t* data;  
};


/**

   Structure: struct net_proto_write_r
   -----------------------------------

   Members are:
   - uuid   : chunk uuid
   - status : write status

**/


struct net_proto_write_r
{
  uint64_t uuid;
  uint8_t status;
};



/**

   Structure: struct net_proto_heartbeat_r
   ---------------------------------------

   Members are:
   - status : server status

**/

struct net_proto_heartbeat_r
{
  uint8_t status;
};


/**

   Structure: struct net_proto_register
   ------------------------------------

   Members are:
   - ipaddr    : chunk server IP address
   - port      : chunk server port
   - available : space available in bytes

**/

struct net_proto_register
{
  uint32_t ipaddr;
  uint16_t port;
  uint64_t available;
};


/**

   Structure: struct net_proto_register_r
   --------------------------------------

   Members are:
   - status : register operation status

**/


struct net_proto_register_r
{
  uint8_t status;
};


/**

   Structure: struct net_proto_chunks_query_r
   ------------------------------------------

   Members are:
   - nchunks : number of chunks
   - uuids   : list of uuids

**/


struct net_proto_chunks_query_r
{
  uint64_t nchunks;
  uint64_t* uuids;
};



#endif
